package Agent;

import java.util.Vector;

import Env.Car;
import Env.Environment;
import Env.Graph;

public class SpeedNutAgent extends AbstractAgent{

	private int[] visits;

	public SpeedNutAgent(int start,  Environment env) {
		super(start, 0, env);
		visits=new int[env.getGraph().n+1];
		visits[start] = 1;
		agentName = "speed nut";
	}

	@Override
	protected void chooseCarAndVertex() throws Exception {
		Graph g=env.getGraph();
		Vector<Integer> connected= g.getAllConnected(position);
		if (env.checkForEffCar(this.position)){
			if ((chosenVertex = env.maxEdge(g, connected,position,history.lastElement())) == -1) return;
			if (g.isEdgeFlooded(position, chosenVertex)){
				//chosen edge is flooded. must choose effective car
				chosenCar=env.maxEffCar(position);
			}else{
				//chosen edge is clear. free to choose a non effective car
				chosenCar=env.maxCar(position);
			}
		}else {
			chosenVertex=env.maxNonFloodedEdge(g, connected,position,history.lastElement());
			chosenCar=env.maxCar(position);
		}
		if (chosenCar!=null && chosenVertex >=0)
			visits[chosenVertex]++;
	}

	public void move() throws Exception{
		super.move();
		if (chosenCar == null || chosenVertex == -1){
			stopFlag = true;
			System.out.println(agentName + " failed to move");
		}else{
			if (visits[position]==3){
				stopFlag=true;
				System.out.println(agentName + " finished");
			}
		}

	}
}
